package com.zhentao.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.model.common.article.dto.ArticleDto;
import com.zhentao.model.common.article.dto.ArticleHomeDto;
import com.zhentao.model.common.dtos.ApArticle;
import com.zhentao.model.common.dtos.ApArticleConfig;
import com.zhentao.model.common.dtos.ApArticleContent;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApArticleService;
import com.zhentao.mapper.ApArticleMapper;
import com.zhentao.service.ArticleFreemarkerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author EWEWW
* @description 针对表【ap_article(文章信息表，存储已发布的文章)】的数据库操作Service实现
* @createDate 2024-06-02 20:45:03
*/
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle>
    implements ApArticleService{
    //设置常量 当页数文字个数传递的值大于50时，使用默认的
    private final static short MAX_PAGE_SIZE = 50;
     @Autowired
     private ApArticleContentMapper articleContentMapper;

     @Autowired
     private ApArticleConfigMapper articleConfigMapper;
     @Autowired
     private ArticleFreemarkerService articleFreemarkerService;

    @Autowired
    private ApArticleMapper articleMapper;
    @Override
    public ResponseResult load(Integer loadtype, ArticleHomeDto dto) {
        //1.校验参数
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);

        //文章频道校验
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("_all_");
        }

        //时间校验
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        //2.查询数据
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, loadtype);
        //3.结果封装
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        return responseResult;
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.校验参数
       if(dto == null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
       }
       //2.判断文章id是否存在，如果不存在，进行添加操作，如果存在进行修改
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        if(dto.getId() == null){
            //保存文章
            articleMapper.insert(apArticle);
            //保存配置
            ApArticleConfig apArticleConfig= new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());

            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);

            articleConfigMapper.insert(apArticleConfig);
            //保存内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.insert(apArticleContent);
        }else {
            //进行修改操作
            //修改文章
            articleMapper.updateById(apArticle);
            //修改内容
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent apArticleContent = articleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.updateById(apArticleContent);
        }
        //异步调用 生成静态的html文件上传到minio当中
        articleFreemarkerService.buildArticleToMinIo(apArticle,dto.getContent());
        //3.返回结果 修改成功应该返回文章id
        return ResponseResult.okResult(apArticle.getId());
    }
}




